<?php
	defined('_JEXEC') or die('Restricted access');
	require_once(CUTEENGINE_BASE_FIELD_PATH."base_field_class.php");
    
	//
    // t_field_lookup
    //
	class t_field_lookup extends t_base_field
	{
        // property declaration
		// protected
		// B
        protected $button_tag = null;
        // K
        protected $key_db_fields = array();
		protected $key_tags_list = array();
		// M
		protected $message_tag = null;
		// T
        protected $tent_tag = null;
        const tag_prefix_item = "_item";
		const tag_prefix_tent = "_tent";
        // V
        protected $value_db_fields = array();
        

        // property declaration
		// public
		// E
		public $example_tag = null;
		// G
		const language_position_per_can_not_be_empty = 1;
		const language_position_last = 2;
		
		// P
             
        const proper_properties_position_per_NULL = 1;
        const proper_properties_position_per_label = 2; 
		const proper_properties_position_per_example = 3; 
		const proper_properties_position_per_is_numeric = 4;
		const proper_properties_position_button_visible = 5;
        const proper_properties_position_last = 6;
        // method declaration
    	// protected
    	// protected
		// A
		protected function adds_check_value_method()
        {
            global $g_javascript_namespace;
         	$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(
				get_class($this),"check_value","a_name","
    var local_name = a_name;
	if (a_name == undefined)
		local_name = this.name;                
    var local_value = this.html_tag(local_name).value.trim();            
    var local_msg_tag = this.html_tag(local_name+'_msg');  
	if ((local_msg_tag == null )||(local_msg_tag == undefined))
		return true; 
	var local_return = true;
    if (this.can_not_be_empty)
    {
		local_return = (this.html_tag(local_name).value.trim() != '');
		
    }
    if (!local_return)
        local_msg_tag.innerHTML = this.can_not_be_empty_msg;
    else
        local_msg_tag.innerHTML = '';    
	return local_return;
    "); 
	
	$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(
			get_class($this),"edit_row_activation","a_tag_value_name,a_tag_value_edit_name,a_active,a_copy_value","
	var local_value_style = '';
	var local_edit_value_style = 'none';
	if (a_active == true)
	{
		local_value_style = 'none';
		local_edit_value_style = '';
	}
	var local_value = set_tag_style_property(['display'],[local_value_style],a_tag_value_name,true);
	var local_edit_value = set_tag_style_property(['display'],[local_edit_value_style],a_tag_value_edit_name,true);
	var local_sibling_value = local_edit_value.nextSibling;
	set_tag_style_property(['display'],[local_edit_value_style],local_sibling_value,true);
	local_sibling_value = local_sibling_value.nextSibling;
	set_tag_style_property(['display'],[local_edit_value_style],local_sibling_value,true);
	
	if (a_copy_value)
	{
		if (a_active == true)
		{
			 
			local_edit_value.value = local_value.innerHTML;
			
		} 	
		else
		{
			
			local_value.innerHTML = local_edit_value.value.trim();
		} 

	}		
	");
        }

		
        // G
		//sets html code to fire event type action on html element
		protected function get_field_event_script($a_event_type,$a_argouments)
        {			
			//echo "a_event_type:$a_event_type<BR>";
			
			switch ($a_event_type) 
            {
                case t_base_field::field_event_type_on_edit:
					$this->main_tag->add_property('onkeydown',t_cuteengine::javascript_instance_name.".".$this->name.".".$this->get_event_name($a_event_type)."($a_argouments)");
					if (strtoupper($this->proper_properties_array[self::proper_properties_position_button_visible]) == 'TRUE')
						$this->button_tag->add_property('onclick',t_cuteengine::javascript_instance_name.".".$this->name.".".$this->get_event_name($a_event_type)."($a_argouments)");
				break;                
			}
		}
static function get_help_proper_property($a_index)
		{
			$local_return = 'NO HELP AVAILABLE';
			/*switch ($a_index) 
            {
                case self::proper_properties_position_:
					$local_return = '';
				break;                
			}*/
			return $local_return;
		}
        static function get_default_proper_property($a_index)
		{
			$local_return = '';
			switch ($a_index) 
            {
                case self::proper_properties_position_button_visible:
					$local_return = 'TRUE';
				break;                
			}
			return $local_return;
		}
		protected function get_proper_properties_position_last()      
		{
            return self::proper_properties_position_last;
        }    
		
        // method declaration
    	// public
        public function __construct($a_parent_part,$a_common_properties_str, $a_proper_properties_str) 
		{
            parent::__construct($a_parent_part,$a_common_properties_str, $a_proper_properties_str);

			
		}

		// C
		// this method permits to add method or function used to manage data in javascript script
		public function create_javascript_initialization_component()
		{
			global $g_javascript_namespace;
			// I
			$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(
				get_class($this),'list_item_selected','event,a_tag_name,a_key_tag_name,a_key_str,a_labels_str',"
    set_tag_property(['value'],[a_key_str],a_key_tag_name,false);
	var local_tag = set_tag_property(['keys'],[a_key_str],a_tag_name,false);
    set_tag_property(['value'],[a_labels_str],local_tag,false);
    set_tag_style_property(['display'],['none'],this.get_tag_name('".self::tag_prefix_tent."',a_tag_name,[local_tag.name]),false);
	local_tag.focus();
	");
			// H
			$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(
                get_class($this->parent_component),"hide_example_tag",'a_tag_name,a_value',"
		var local_tag = this.html_tag(a_tag_name);
		var local_tag_example = this.html_tag(a_tag_name+'_example');
		
		if ((a_value == false) && (local_tag.value == ''))
		{
			local_tag_example.ce_extension.change_style('display','block'); 
		}
		else
		{
			local_tag_example.ce_extension.change_style('display','none'); 
		}	
		");
	
			// S
			$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(
                get_class($this->parent_component),"set_focus_tag",'a_tag_name',"
		var local_tag = this.html_tag(a_tag_name);
		local_tag.focus();
		");				
			$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(
				get_class($this),"set_tent_value","a_value,a_tag_name","
	var local_tent = window.cuteengine.html_tag(this.get_tag_name('".self::tag_prefix_tent."',a_tag_name,[]));
    local_tent.innerHTML = a_value;
	if (a_value.trim() != '')
		set_tag_style_property(['display'],[''],local_tent,true);"); 
		
		//STYLE
		$this->add_style_sheet_property(
			".".$this->get_tag_name(self::tag_prefix_tent,array())." ul",'position','absolute');
		$this->add_style_sheet_property(
			".".$this->get_tag_name(self::tag_prefix_tent,array())." ul",'width','100%');
		$this->add_style_sheet_property(
			".".$this->get_tag_name(self::tag_prefix_tent,array())." ul",'z-index','100');
		$this->tent_tag->class_name	= $this->get_tag_name(self::tag_prefix_tent,array());
		}
		public function create_tags_structure()
        {
			global $g_javascript_namespace;
			global $g_ambient_namespace;
            //echo __METHOD__."<BR>";
            //echo "this->proper_properties_array[self::proper_properties_position_per_NULL]: ".$this->proper_properties_array[self::proper_properties_position_per_NULL]."<br>";
            if (($this->proper_properties_array[self::proper_properties_position_per_NULL] == 1) ||
                (strtoupper($this->proper_properties_array[self::proper_properties_position_per_NULL]) == "TRUE"))
                $local_can_not_be_empty = "true";
            else
                $local_can_not_be_empty = "false";
            //echo "local_can_not_be_empty:$local_can_not_be_empty<br>";    
			$local_can_not_be_empty_msg = addslashes($this->lang[self::language_position_per_can_not_be_empty]);	
            parent::create_tags_structure(
				// ARGUMENTS DECLARATIONS
                "a_can_not_be_empty,a_can_not_be_empty_msg",
                // ARGUMENTS VALUES
                "$local_can_not_be_empty,'$local_can_not_be_empty_msg'");
			// proprierties

			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'can_not_be_empty',"a_can_not_be_empty",false);
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'can_not_be_empty_msg',"a_can_not_be_empty_msg",false);	
            // STYLE            
			$local_name = $this->common_properties_array[t_base_field::common_properties_position_per_field_name];
			// LABEL
            $local_label = $this->proper_properties_array[self::proper_properties_position_per_label];
            if (trim($local_label) != '')
            {
                $local_current_tag = parent::add_tag(t_base_tag_type::btt_label,$local_name . "_label");
                $this->label_tag = $local_current_tag;
				$local_current_tag->set_text_to_include($local_label);
				$local_current_tag->add_text_to_include($local_input_label_text);
				$local_current_tag->class_name = $this->name . "_input_label_class";				
            }
			
			// INPUT
            $local_current_tag = parent::add_tag(t_base_tag_type::btt_input,
                $local_name);
			$local_current_tag->class_name = $this->name . "_input_class";
			$this->main_tag = $local_current_tag;
			// BUTTON
			if (strtoupper($this->proper_properties_array[self::proper_properties_position_button_visible]) == 'TRUE')
			{
				
				$local_current_tag = parent::add_tag(t_base_tag_type::btt_button,
                $local_name."_btn");
				$local_current_tag->class_name = "lookup_button_class";
				$this->add_style_sheet_property(".lookup_button_class","width","15px");
				$this->add_style_sheet_property(".lookup_button_class","height","20px");
				$this->add_style_sheet_property(".lookup_button_class","background-image","url('".$g_ambient_namespace->get_absolute_web_style_file_path()."doc-row-down.png')");
				if (strtoupper($this->proper_properties_array[self::proper_properties_position_button_visible]) == 'TRUE')
					$this->button_tag = $local_current_tag;

				//$local_current_tag->set_text_to_include('V');
			}
			//$local_current_tag->add_event_as_local_method($local_current_tag->name,'onfocus',"hide_example_tag",$this->delayed_position,"'$local_current_tag->name',true");
			//$local_current_tag->add_event_as_local_method($local_current_tag->name,'onblur',"hide_example_tag",$this->delayed_position,"'$local_current_tag->name',false");

			
            $this->get_field_event_script(t_base_field::field_event_type_on_edit,"event");
            $this->default_start_tag = $this->main_tag;
            $this->default_result_tag = $this->main_tag;    
            // EXAMPLE
            $local_example = $this->proper_properties_array[self::proper_properties_position_per_example];
			if (trim($local_example) != '')
            {
				$this->main_tag->add_event_as_local_method($local_current_tag->name,'onfocus',"hide_example_tag",$this->delayed_position,"'$local_current_tag->name',true");
				$this->main_tag->add_event_as_local_method($local_current_tag->name,'onblur',"hide_example_tag",$this->delayed_position,"'$local_current_tag->name',false");
                $local_current_tag = parent::add_tag(t_base_tag_type::btt_div,$local_name . "_example");
                $this->example_tag = $local_current_tag;
				$local_current_tag->set_text_to_include($local_example);
				$local_current_tag->class_name = $this->name . "_input_example_class";
				$g_javascript_namespace->header_script_generator->add_style_sheet_property(".$local_current_tag->name",'position','relative');
				$local_current_tag->add_event_as_local_method($local_current_tag->name,'onclick',"set_focus_tag",$this->delayed_position,"'".$this->main_tag->name."'");

            }
            $this->tent_tag = parent::add_tag(t_base_tag_type::btt_div,
                $this->get_tag_name(self::tag_prefix_tent,array()));
			$this->tent_tag->add_style_property('display','none');
			//print_r($this->value_db_fields);
			// message tag
			$local_current_tag = parent::add_tag(t_base_tag_type::btt_label,$local_name . "_msg");
            $this->message_tag = $local_current_tag;
			$this->message_tag->class_name = t_base_field::error_msg_class_name;	
            // method
            $this->adds_check_value_method();			
        }
        // G
        public function get_event_name($a_event_type)
        {

            return /*get_class($this)*/$this->name . "_event_".$a_event_type;
        }
		public function get_included_in_html_structure($a_name,$a_display,$a_key_tag_name)
		{
			parent::get_included_in_html_structure($a_name);
			if ($a_display)
			{
				$this->main_tag->add_style_property('display','');
				$this->tent_tag->add_style_property('display','');
				if (strtoupper($this->proper_properties_array[self::proper_properties_position_button_visible]) == 'TRUE')
					$this->button_tag->add_style_property('display','');
			}
			else
			{
				$this->main_tag->add_style_property('display','none');
				$this->tent_tag->add_style_property('display','none');
				if (strtoupper($this->proper_properties_array[self::proper_properties_position_button_visible]) == 'TRUE')
					$this->button_tag->add_style_property('display','none');
			}	
			$this->key_tags_list[$a_name] = $a_key_tag_name;
			$local_button_tag = '';
			if (strtoupper($this->proper_properties_array[self::proper_properties_position_button_visible]) == 'TRUE')
				$local_button_tag = $this->button_tag->generate_html_code_with_new_name($a_name);
			//echo "a_name: $a_name - a_key_tag_name: $a_key_tag_name<br>";
			return ($this->main_tag->generate_html_code_with_new_name($a_name) .
				$local_button_tag.
				$this->tent_tag->generate_html_code_with_new_name($this->get_tag_name(self::tag_prefix_tent,array(),$a_name)).
                $this->message_tag->generate_html_code_with_new_name($a_name.t_field_text::postfix_message_tag_name));
		}
		
		// gets result script for action based on event type
        public function get_result_action_script($a_event_type,$parent_event_type,
            $a_parameter,$a_content_loader_result)
		{
	
			switch ($a_event_type) 
            {
                /***********  t_base_field::field_event_type_on_edit ***********/
                case t_base_field::field_event_type_on_edit:
				break;
                /***********  t_base_field::field_event_type_on_cancel ***********/
                case t_base_field::field_event_type_on_cancel:
					
				break;
            }
        }
        
        public function get_result_tag()
        {	
            parent::get_result_tag();
            return $this->main_tag;
		} 
        public function get_start_tag()
        {	
            parent::get_start_tag();
            return $this->main_tag;
		} 		
		
        public function get_tags_list_structure($a_query_parameter,$a_current_row_no,$a_rows_count,
            $a_argouments)
        {	
           $local_result =  $a_argouments[0];
           $local_destination =  $a_argouments[1]; 
			if ($a_query_parameter->is_first_record())
            {
                if ((count($this->key_db_fields) == 0) && (count($this->value_db_fields) == 0))
                {
                    $a_query_parameter->execute_function_from_fields_result($this,'initialize_db_fields');
                }    
                $a_query_parameter->function_result = "<ul>";
            }    
            // keys string
			$local_key_str = '';
			print_r($this->key_db_fields);
			foreach ($this->key_db_fields as $local_field) 
			{
				if ($local_key_str == '')
					$local_key_str = $a_query_parameter->sql_values[$local_field];
				else	
					$local_key_str .= ", ".$a_query_parameter->sql_values[$local_field];
			} 
            // labels string
			$local_labels_str = '';
			$local_labels_str2 = '';
			foreach ($this->value_db_fields as $local_field) 
			{
				if ($local_labels_str == '')
				{
					$local_labels_str = $a_query_parameter->sql_values[$local_field];
					$local_labels_str2 = $a_query_parameter->sql_values[$local_field];
				}	
				else
				{
					$local_labels_str .= " - ".$a_query_parameter->sql_values[$local_field];
					$local_labels_str2 .= ", ".$a_query_parameter->sql_values[$local_field];
				}	
			}		
			$local_labels_str2 = str_replace('\\', '\\\\',$local_labels_str2);
			$local_labels_str2 = str_replace("'", "\\\'",$local_labels_str2);
			//$local_labels_str2 = str_replace('"', '\\\"',$local_labels_str2);
			
			echo "local_labels_str2:$local_labels_str2-";
			//
            $a_query_parameter->function_result .= "<li class=".$this->get_tag_name(self::tag_prefix_item,array())." onclick='". t_cuteengine::javascript_instance_name.".".$this->name."." ."list_item_selected(event,\"".$local_destination."\",\"".$this->key_tags_list[$local_destination]."\",\"$local_key_str\",\"$local_labels_str2\")'>". $local_labels_str ."</li>";
            if ($a_query_parameter->is_last_record())
            {
                $a_query_parameter->function_result .= "</ul>";
				$local_result->add_function_result(
					"window.cuteengine.".$this->name,"set_tent_value","'" .            
                    str_replace("'", "\'",$a_query_parameter->function_result)."',a_req.source_tag_name",""); 
               // $local_result->add_function_result(
                //    "","","a_req.source_tag_name","var local_tent = window.cuteengine.html_tag(window.cuteengine.".$this->name.".get_tag_name('".self::tag_prefix_tent."',[arguments[0]]));". 
    //"local_tent.innerHTML = '" .            
      //              str_replace("'", "\'",$a_query_parameter->function_result)."'");  
//                $local_result->add_function_result(
  //                  "","","","set_tag_style_property(['display'],[''],'".$this->get_tag_name(self::tag_prefix_tent,array()) ."'),true;");  
                    
            }        
        } 	
		public function get_tags_list_structure_from_empty_query($a_argouments)
        {	
           $local_result =  $a_argouments[0];
           $local_destination =  $a_argouments[1];
		
			$local_result->add_function_result(
				"window.cuteengine.".$this->name,"set_tent_value","' ',a_req.source_tag_name","");  
			 
			
		}

        // I
		public function initialize_db_fields($a_query_parameter,$a_field,$a_field_no,$a_fields_count,
                            $a_row_no,$local_rows_count)
        {
			 //echo  "a_field_no: $a_field_no - main_table_name: $a_query_parameter->main_table_name - a_field->name: $a_field->name<br>";
            if (($a_field->primary_key == 1) || (($a_query_parameter->main_table_name == '') && ($a_field_no == 0)) ) 
                // key_db_fields
                $this->key_db_fields[] = $a_field->name;
            else
            {
                //echo "a_field->name:".$a_field->name."<br>";
                // value_db_fields
                $this->value_db_fields[] = $a_field->name;
            }    
		}		
		public function initialize_field_event($a_event_type)
        {			
			global $g_javascript_namespace;
            global $g_strings_utility_namespace;
			$local_event = $this->events[$a_event_type];
			//echo "local_event->event_name: ".$local_event->event_name."<br>";
			
			
			switch ($local_event->event_type) 
            {
                /***********  t_base_field::field_event_type_on_edit  ***********/                
				case t_base_field::field_event_type_on_edit:
					if ($this->get_is_numeric())
					{
						$local_is_input_ok =  "var isInputOk = ((keyCode >= 48) && (keyCode <= 57)) ||
							((keyCode >= 96) && (keyCode <= 105));";
						$local_is_to_be_processed =  "is_to_be_processed = ((local_argoument != '') && ((keyCode == 46) || (keyCode == 8) || 
		((keyCode >= 48) && (keyCode <= 57)) ||
		((keyCode >= 96) && (keyCode <= 105))));";
					}		
					else
					{
						$local_is_input_ok = 'var isInputOk = true;';
						$local_is_to_be_processed =  "is_to_be_processed = true;";//((local_argoument != ''));";
					}	
					$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(
						get_class($this),$local_event->event_name,'event',"
        var keyCode = (event.which === undefined) ? event.keyCode : event.which;
        var local_target = event.target ? event.target : event.srcElement;
		if (local_target.tagName == 'BUTTON')
		{
			local_target = local_target.previousSibling;
			keyCode = null;
		}
        var local_source_tag = local_target.id;
		var local_destination_tag = local_target.id;
		$local_is_input_ok
        if (isInputOk)
        {		
            var is_to_be_processed = false;
            var local_start = get_selection_start(local_target);
            var local_end = get_selection_end(local_target);
            if (keyCode != null)
				var local_argoument = local_target.value.substr(0,local_start) + String.fromCharCode(keyCode) +
					local_target.value.substr(local_start,local_target.value.length)
			else
				var local_argoument = local_target.value;			
            if (local_end != local_start)
            {
                local_argoument = local_target.value.replace(
                    local_target.value.substring(local_start,local_end),'');
                if ((keyCode != 8) && (keyCode != 46))
                    local_argoument = local_argoument.substr(0,local_start) + String.fromCharCode(keyCode) +
                        local_argoument.substr(local_start,local_target.value.length);
            }
            else if (keyCode == 8)
            {
                local_argoument = local_target.value.substr(0,local_start-1) + 
                    local_target.value.substr(local_start,local_target.value.length);
            }
            else if (keyCode == 46)
            {
                local_argoument = local_target.value.substr(0,local_start) + 
                    local_target.value.substr(local_start+1,local_target.value.length);
            }
            $local_is_to_be_processed
            //
            local_argoument = local_argoument +'".t_url_parameter::separator_level_0."' + local_destination_tag; 
			if (is_to_be_processed)
            {
                ".
                $local_event->body_code.
                "
            }
            else
            {
            }
        }
        else
        {
            return false;
        }
        ");
				break;

			}	
            
		}
        

		
		public function get_language_position_last()
        {
            return self::language_position_last;
        }
		public function get_is_numeric()
        {			
			return (strtoupper($this->proper_properties_array[self::proper_properties_position_per_is_numeric]) === 'TRUE');
				
		}

		
        
    }
?>    